package com.jcark.ui.view.span;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ImageSpan;

import androidx.annotation.NonNull;

/**
 * Description:可设置图片居中的ImageSpan <br>
 *
 * @author King.Zi<br>
 * @date 2022/7/28<br>
 * Email: 78688689@qq.com
 */
public class JImageSpan extends ImageSpan {
    private int mAlignment = 0;
    private int mMarginLeft = 0;
    private int mMarginRight = 10;

    public JImageSpan(@NonNull Bitmap b) {
        super(b);
    }

    public JImageSpan(@NonNull Bitmap b, int verticalAlignment) {
        super(b, verticalAlignment);
    }

    public JImageSpan(@NonNull Context context, @NonNull Bitmap bitmap) {
        super(context, bitmap);
    }

    public JImageSpan(@NonNull Context context, @NonNull Bitmap bitmap, int verticalAlignment) {
        super(context, bitmap, verticalAlignment);
    }

    public JImageSpan(@NonNull Drawable drawable) {
        super(drawable);
    }

    public JImageSpan(@NonNull Drawable drawable, int verticalAlignment, int marginLeft, int marginRight) {
        super(drawable);
        mAlignment = verticalAlignment;
        mMarginLeft = marginLeft;
        mMarginRight = marginRight;
    }

    public JImageSpan(@NonNull Drawable drawable, int verticalAlignment) {
        super(drawable, verticalAlignment);
    }

    public JImageSpan(@NonNull Drawable drawable, @NonNull String source) {
        super(drawable, source);
    }

    public JImageSpan(@NonNull Drawable drawable, @NonNull String source, int verticalAlignment) {
        super(drawable, source, verticalAlignment);
    }

    public JImageSpan(@NonNull Context context, @NonNull Uri uri) {
        super(context, uri);
    }

    public JImageSpan(@NonNull Context context, @NonNull Uri uri, int verticalAlignment) {
        super(context, uri, verticalAlignment);
    }

    public JImageSpan(@NonNull Context context, int resourceId) {
        super(context, resourceId);
    }

    public JImageSpan(@NonNull Context context, int resourceId, int verticalAlignment) {
        super(context, resourceId, verticalAlignment);
        mAlignment = verticalAlignment;
    }

    public JImageSpan(@NonNull Context context, int resourceId, int verticalAlignment, int marginLeft,
                      int marginRight) {
        super(context, resourceId, verticalAlignment);
        mAlignment = verticalAlignment;
        mMarginLeft = marginLeft;
        mMarginRight = marginRight;
    }

    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end,
                       Paint.FontMetricsInt fontMetricsInt) {
        Drawable drawable = getDrawable();
        Rect rect = drawable.getBounds();
        if (null != fontMetricsInt) {
            Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
            int fontHeight = fmPaint.descent - fmPaint.ascent;
            int drHeight = rect.bottom - rect.top;
            int centerY = fmPaint.ascent + fontHeight / 2;
            fontMetricsInt.ascent = centerY - drHeight / 2;
            fontMetricsInt.top = fontMetricsInt.ascent;
            fontMetricsInt.bottom = centerY + drHeight / 2;
            fontMetricsInt.descent = fontMetricsInt.bottom;
        }
        return mMarginLeft + rect.right + mMarginRight;
    }

    @Override
    public void draw(Canvas canvas, CharSequence text,
                     int start, int end, float x,
                     int top, int y, int bottom, Paint paint) {

        if (DynamicDrawableSpan.ALIGN_CENTER == mAlignment) {
            // image to draw
            Drawable b = getDrawable();
            canvas.save();

            Paint.FontMetricsInt fm = paint.getFontMetricsInt();
            int transY = (y + fm.descent + y + fm.ascent) / 2 - b.getBounds().bottom / 2;
            canvas.translate(mMarginLeft + x, transY);
            b.draw(canvas);
            canvas.restore();
        } else {
            super.draw(canvas, text, start, end, x, top, y, bottom, paint);
        }
    }
}
